package com.example.administrator.demoa;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class MyContentProvider extends ContentProvider {
    private MyOpenHelper helper;
    //授权信息
    public static final String AUTHORITY = "com.hjn.xn";
    public static final int stu_dir = 1;
    public static final int score_dir = 2;
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        uriMatcher.addURI(AUTHORITY, "student", stu_dir);
        uriMatcher.addURI(AUTHORITY, "score", score_dir);
    }

    public MyContentProvider() {
    }

    @Override
    public boolean onCreate() {
        helper = new MyOpenHelper(getContext(), "stu.db", null, 1);
        return true;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = helper.getWritableDatabase();
        switch (uriMatcher.match(uri)){
            case 1:
                db.insert("student", null, values);
                break;
            case 2:
                db.insert("score", null, values);
                break;
        }

        db.close();
        return null;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = null;
        switch (uriMatcher.match(uri)){
            case 1:
                cursor = db.query("student", projection, selection, selectionArgs,
                        null, null, sortOrder);
                break;
            case 2:
                cursor = db.query("score", projection, selection, selectionArgs,
                        null, null, sortOrder);
                break;
        }

        //发送广播
        getContext().sendBroadcast(new Intent("STU_CHANGE"));
        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int rows = 0;
        switch (uriMatcher.match(uri)){
            case 1:
                rows = db.update("student", values, selection, selectionArgs);
                break;
            case 2:
                rows = db.update("score", values, selection, selectionArgs);
                break;
        }

        db.close();
        return rows;
    }


    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int rows = 0;
        switch (uriMatcher.match(uri)){
            case 1:
                rows = db.delete("student", selection, selectionArgs);
                break;
            case 2:
                rows = db.delete("score", selection, selectionArgs);
                break;
        }

        db.close();
        return rows;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}
